package com.pg.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期转换类
 *
 * @author L
 */
public class DateFormat {

    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    public static String date2String(Date date) {
        return sdf1.format(date);
    }

    public static String date2String1(Date date) {
        return sdf2.format(date);
    }

    public static String date2String2(Date date) {
        return sdf3.format(date);
    }

    public static Date String2Date1(String dateTime) {
        Date date = null;
        try {
            sdf2.setLenient(false);
            date = sdf2.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static boolean String2Date1IsTrue(String dateTime) {
        try {
            sdf2.setLenient(false);
            sdf2.parse(dateTime);
            return true;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Date String2Date2(String dateTime) {
        Date date = null;
        try {
            sdf1.setLenient(false);
            date = sdf1.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static boolean String2Date2IsTrue(String dateTime) {
        try {
            sdf1.setLenient(false);
            sdf1.parse(dateTime);
            return true;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }
    public static long getDatePoor(Date startDate, Date endDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
//         long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = startDate.getTime() - endDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒
//        long sec = diff % nd % nh % nm / ns;
        // 输出结果
//        return day + "天" + hour + "小时" + min + "分钟";
        return day * 24 * 60 + hour * 60 + min;
    }
}
